package es.yporqueno.cargadores {
	
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.utils.getQualifiedClassName;

	public class AbstractCargadorMedia extends AbstractCargador {
		
		protected var _conexion:NetConnection;
		protected var _stream:NetStream;
		protected var _url:String;
		protected var _miVideo:Video;
		protected var _posH:String
		protected var _posV:String;
		
		public function AbstractCargadorMedia() {
			// Clase Abstracta. No puede ser instanciada
			if (getQualifiedClassName(this) == "com.uveproducers.cargadores::AbstractCargadorMedia") {
				throw new Error("AbstractCargadorMedia es una clase abstracta y no puede ser instanciada");
				return;
			}
		}
		
		// Funciones Publicas -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		 public function load(url:String, alineacion:String = "TOP_LEFT"):void{
		 	_url = url;
		 	
		 	_posV = alineacion.split("_")[0];
			_posH = alineacion.split("_")[1];
			
			_conexion.connect(null);
		} 
		
		
		// Funciones Privadas -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		protected function init():void{
			_conexion = new NetConnection();
			_conexion.addEventListener(NetStatusEvent.NET_STATUS, conexionStatus);
		}
		
		override protected function loaderComplete(e:Event):void{
			
			super.loaderComplete(e);
		}
		
		// 
		protected function conexionStatus(e:NetStatusEvent):void{
			trace("NetStatusEvent: " + e.info.code);
			switch(e.info.code){
				case "NetConnection.Connect.Success":
					conectarStream();
			}
		}
		
		// 
		//protected function loaderProgress(e:ProgressEvent):void{
			/* var p:Number = e.bytesLoaded/e.bytesTotal;
			p = (e.bytesTotal == 0) ? 0 : p;
			dispatchEvent(new CargadorProgressEvent(CargadorProgressEvent.PROGRESS, p)); */
		//}
		
		// 
		//protected function loaderOpen(e:Event):void{
			//
		//}
		
		// 
		//protected function loaderInit(e:Event):void{
			//
		//}
		
		// 
		//protected function loaderUnload(e:Event):void{
			//
		//}
		
		// 
		private function conectarStream():void{
			_stream = new NetStream(_conexion);
			_stream.addEventListener(NetStatusEvent.NET_STATUS, conexionStatus);
			_stream.client = new CustomClient();
			_miVideo = new Video(480, 270);
			_miVideo.attachNetStream(_stream);
			_stream.play(_url);
			//_stream.pause();
			_data = _miVideo;
			// Coloco la imagen en la posicion indicada, en horizontal y vertical
			if (_posH == "LEFT") {
				//
			} else if (_posH == "CENTER") {
				_miVideo.x -= _miVideo.width / 2;
			} else if (_posH == "RIGHT") {
				_miVideo.x -= _miVideo.width;
			}

			if (_posV == "TOP") {
				//
			} else if (_posV == "CENTER") {
				_miVideo.y -= _miVideo.height / 2;
			} else if (_posV == "BOTTOM") {
				_miVideo.y -= _miVideo.height;
			}
			super.loaderComplete(null);
		}
		
		private function limpiar():void{
			//_loader = null;
		}
		
		
		
	}
}



class CustomClient {

	public function onMetaData(info:Object):void {
		trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
	}

	public function onCuePoint(info:Object):void {
		trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
	}
}





